package com.fetch;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import com.fetch.common.Configuration;
import com.fetch.common.Constants;
import com.fetch.common.LOG;
import com.fetch.common.StringUtils;
import com.fetch.ui.LogPane;
import com.fetch.ui.Status;

/**
 *整个程序的入口 
 */
public class Main extends JFrame {
	private static final long serialVersionUID = 1L;
	
	public static String COOKIE = "";
	public static Main self;

	public boolean started = false; //是否已开始，已开始为true
	
	private JTextField cookieField = new JTextField();
	private JButton startBtn = new JButton("开始");
	private LogPane logPane = new LogPane();
	public Status companyStatus = new Status("工商企业");
	public Status personStatus = new Status("个体户    ");

	private Crawler companyCrawler = null;
	private Crawler personCrawler = null;
	
	private int completeCount = 0;

	public Main(){
		self = this;
	}
	
	/**
	 * 初始化
	 */
	private void init(){
		this.setTitle("图片抓取器");
		this.setSize(900, 600); 
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  //关闭窗口的时候，程序退出
		this.setLocationRelativeTo(null);
		this.setLayout(null);
		this.setResizable(false);
		this.setVisible(true); 
		
		cookieField.setBounds(10, 10, 380, 30);
		startBtn.setBounds(400, 10, 60, 30);
		startBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if(started){
					stop();
				}else{
					start();
				}
			}
		});
		
		companyStatus.setBounds(500, 3, 400, 20);
		personStatus.setBounds(500, 25, 400, 20);
		
		JScrollPane scrollPane = new JScrollPane(logPane);
		scrollPane.setBounds(0, 50, 895, 520); //470
		
		this.add(cookieField);
		this.add(startBtn);
		this.add(companyStatus);
		this.add(personStatus);
		this.add(scrollPane);
		this.repaint();
		
		LOG.logPane = logPane;
	}
	
	/**
	 * 开始
	 */
	private void start() {
		if(Configuration.load()){
			COOKIE = cookieField.getText();
			if(StringUtils.isEmpty(COOKIE)){
				LOG.error("请填写cookie信息");
				return;
			}
			Boolean b = checkCookie();
			if(null == b){
				LOG.error("请检查网络");
			}else if(false == b){
				LOG.error("cookie信息错误，请重新填写");
			}else{
				companyCrawler = new Crawler(Constants.CATEGORY_COMPANY, companyStatus);
				companyCrawler.start();
				
				personCrawler = new Crawler(Constants.CATEGORY_PERSON, personStatus);
				personCrawler.start();
				started = true;
				updateUi();
				LOG.info("已开始");
			}
		}
	}
	
	/**
	 * 停止
	 */
	private void stop() {
		companyCrawler.stop();
		personCrawler.stop();
		started = false;
		updateUi();
		LOG.info("已停止");
	}
	
	public void complete(){
		completeCount++;
		if(2 == completeCount){
			stop();
		}
	}
	
	/**
	 * 更新ui
	 */
	private void updateUi(){
		if(started){
			startBtn.setText("停止");
		}else{
			startBtn.setText("开始");
		}
	}
	
	private Boolean checkCookie(){
		HttpClient client = new HttpClient();
		PostMethod postMethod = null;
		try{
			postMethod = new PostMethod("http://218.4.189.213:8001/CorpSel/Corp_info.aspx?corp_id=1");
			postMethod.addRequestHeader("Cookie", COOKIE);
			client.executeMethod(postMethod);
			String result = postMethod.getResponseBodyAsString();
			return (result.indexOf("/login.aspx")==-1 ? true:false);
		}catch (Exception e) {
			e.printStackTrace();
		}finally{
			postMethod.releaseConnection();
			client.getHttpConnectionManager().closeIdleConnections(0);
		}
		return null;
	}
	
	public static void main(String[] args) {
		Main main = new Main();
		main.init();
	}
	
	
}
